﻿/*
 * EmeCoder v1.2.1.168
 * 作者：Administrator/PC4APPLE
 * 时间：2013-08-22 13:28:39
 * 版权：版权所有 (C) Eme Development Team 2013
*/

﻿using System;
using System.ComponentModel;
using System.Xml.Serialization;
using HmFramework.Data;
using HmFramework.Log;

namespace CuteSprite.Eme.Data.Settlement
{
	/// <summary>公布价方案</summary>
	[Serializable]
	[ModelCheckMode(ModelCheckModes.CheckTableWhenFirstUse)]
	public class SettlementSolution : SettlementSolution<SettlementSolution> { }

	/// <summary>公布价方案</summary>
	public partial class SettlementSolution<TEntity> : Entity<TEntity> where TEntity : SettlementSolution<TEntity>, new()
	{
		#region 对象操作﻿

		static SettlementSolution()
		{
			// 用于引发基类的静态构造函数，所有层次的泛型实体类都应该有一个
			TEntity entity = new TEntity();
		}

		/// <summary>验证数据，通过抛出异常的方式提示验证失败。</summary>
		/// <param name="isNew"></param>
		public override void Valid(Boolean isNew)
		{
			// 这里验证参数范围，建议抛出参数异常，指定参数名，前端用户界面可以捕获参数异常并聚焦到对应的参数输入框
			//if (MatchHelper.StrIsNullOrEmpty(Name)) throw new ArgumentNullException(_.Name, _.Name.DisplayName + "无效！");
			//if (!isNew && ID < 1) throw new ArgumentOutOfRangeException(_.ID, _.ID.DisplayName + "必须大于0！");

			// 建议先调用基类方法，基类方法会对唯一索引的数据进行验证
			base.Valid(isNew);

			// 在新插入数据或者修改了指定字段时进行唯一性验证，CheckExist内部抛出参数异常
			//if (isNew || Dirtys[__.Name]) CheckExist(__.Name);

			if (isNew)
			{
				//if (!Dirtys[__.CreateOn]) { CreateOn = DateTime.Now; }
				CreateOn = DateTime.Now;
			}
			else if (HasDirty)
			{
				//if (!Dirtys[__.ModifiedOn]) { ModifiedOn = DateTime.Now; }
				ModifiedOn = DateTime.Now;
			}
		}

		/// <summary>首次连接数据库时初始化数据，仅用于实体类重载，用户不应该调用该方法</summary>
		[EditorBrowsable(EditorBrowsableState.Never)]
		protected override void InitData()
		{
			base.InitData();

			// InitData一般用于当数据表没有数据时添加一些默认数据，该实体类的任何第一次数据库操作都会触发该方法，默认异步调用
			// Meta.Count是快速取得表记录数
			if (Meta.Count > 0) { return; }

			// 需要注意的是，如果该方法调用了其它实体类的首次数据库操作，目标实体类的数据初始化将会在同一个线程完成
			HmTrace.WriteDebug("开始初始化{0}[{1}]数据……", typeof(TEntity).Name, Meta.Table.DataTable.DisplayName);

			var entity = new TEntity();
			entity.Name = "默认方案";
			entity.Code = "DS";
			entity.ModifiedOn = DateTime.Now;
			entity.ModifiedUserID = 1;
			entity.ModifiedBy = "Administrator";
			entity.CreateOn = DateTime.Now;
			entity.CreateUserID = 1;
			entity.CreateBy = "Administrator";
			entity.SaveWithoutValid();

			HmTrace.WriteDebug("完成初始化{0}[{1}]数据！", typeof(TEntity).Name, Meta.Table.DataTable.DisplayName);
		}

		/// <summary>已重载。删除关联数据</summary>
		/// <returns></returns>
		protected override int OnDelete()
		{
			if (BillingSolutions != null) { BillingSolutions.Delete(); }

			return base.OnDelete();
		}

		#endregion 对象操作﻿

		#region 扩展属性﻿

		//[NonSerialized]
		//private EntityList<BillingSolution> _BillingSolutions;
		/// <summary>该公布价方案所拥有的计费方案集合</summary>
		[XmlIgnore]
		public EntityList<BillingSolution> BillingSolutions
		{
			get
			{
				return Extends.GetExtend<BillingSolution, EntityList<BillingSolution>>("BillingSolutions", es => BillingSolution.FindAllBySettlementSolutionID(ID));
				//if (_BillingSolutions == null && ID > 0 && !Dirtys.ContainsKey("BillingSolutions"))
				//{
				//	_BillingSolutions = BillingSolution.FindAllBySettlementSolutionID(ID);
				//	Dirtys["BillingSolutions"] = true;
				//}
				//return _BillingSolutions;
			}
			set { Extends.SetExtend<BillingSolution>("BillingSolutions", value); }
		}

		#endregion 扩展属性﻿

		#region 扩展查询﻿

		/// <summary>根据方案代码查找</summary>
		/// <param name="code">方案代码</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static TEntity FindByCode(String code)
		{
			if (Meta.Count > 1000)
				return Find(_.Code, code);
			else // 实体缓存
				return Meta.Cache.Entities.Find(__.Code, code);
			// 单对象缓存
			//return Meta.SingleCache[code];
		}

		/// <summary>根据主键查找</summary>
		/// <param name="id">主键</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static TEntity FindByID(Int32 id)
		{
			if (Meta.Count > 1000)
				return Find(_.ID, id);
			else // 实体缓存
				return Meta.Cache.Entities.Find(_.ID, id);

			// 单对象缓存
			//return Meta.SingleCache[id];
		}

		#endregion 扩展查询﻿

		#region 扩展操作

		#endregion 扩展操作

		#region 业务

		#endregion 业务
	}
}